package view;
import javax.swing.AbstractButton;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.net.URL;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
import model.Passenger;
import model.Plane;
import control.PassengerController;
import java.util.ArrayList;

public class mytrips {
    static JFrame jfra;
    static JPanel jpan1, jpan2, jpan3, jpan4, jpan5, jpan6, jpan7, jpan8,jpan9,jpan10,jpan11,jpan12,jpan13,jpan14;
    static JLabel lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9;
    static Font fnt1, fnt2, fnt3, fnt4, fnt5, fnt6, fnt7, fnt8, fnt9;
    static JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
    public static LogIn logIn = new LogIn();


    public mytrips() {
        jfra = new JFrame();
        ImageIcon background;
        JPanel myPanel;
        JLabel label;
        JLabel company, MyTripLabel, LogInLabel, ServiceLabel, VIPLabel, ModifyLabel, ContactLabel;

        //background
        background = new ImageIcon("src/view/image/background.png");
        label = new JLabel(background);
        label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
        myPanel = (JPanel) jfra.getContentPane();
        myPanel.setOpaque(false);
        myPanel.setLayout(new FlowLayout());
        jfra.getLayeredPane().setLayout(null);

        //top bar
        jpan1 = new JPanel();
        company = new JLabel("          Welcome                    ", JLabel.LEFT);
        Font fnt = new Font("Serief", Font.BOLD, 40);// Setting the size and style of the font.
        company.setFont(fnt);
        MyTripLabel = new JLabel("MyTrips          ", JLabel.RIGHT);
        MyTripLabel.setFont(new Font("Serief", Font.BOLD, 20));
        LogInLabel = new JLabel("LogIn          ", JLabel.RIGHT);
        ServiceLabel = new JLabel("Service         ", JLabel.RIGHT);
        VIPLabel = new JLabel("VIP         ", JLabel.RIGHT);
        ModifyLabel = new JLabel("Modify         ", JLabel.RIGHT);
        ContactLabel = new JLabel("Contact           ", JLabel.RIGHT);
        company.setForeground(Color.blue);


        jpan1.add(company);
        jpan1.add(MyTripLabel);
        jpan1.add(LogInLabel);
        jpan1.add(ServiceLabel);
        jpan1.add(VIPLabel);
        jpan1.add(ModifyLabel);
        jpan1.add(ContactLabel);
        jpan1.setVisible(true);




        Color  qianlan = new Color(100 ,149 ,237);

        JPanel panel1=new JPanel();  // A panel to put flight selection
        int windowsWedth = 500;
        int windowsHeight = 500;
        JPanel panel0 = new JPanel(new BorderLayout());
        panel0.setBackground(qianlan);
       // panel1.setBackground(Color.blue);
        panel1.setLayout(new GridLayout(1,3,20,10));
        panel1.setOpaque(true);
        panel1.setSize(300,300);
        JLabel label1=new JLabel("                Please Choose a flight");
        Font fnt2 = new Font("Serief", Font.BOLD, 30);// Setting the size and style of the font.
        label1.setFont(fnt2);
        label1.setForeground(Color.white);
        ButtonGroup group=new ButtonGroup();
        panel0.add(BorderLayout.NORTH,label1);

        PassengerController controller = new PassengerController();
        Passenger psg = controller.queryByBookNumber(logIn.bookNumber);
        // System.out.println(me);
        ArrayList<Plane> myList= controller.queryPlaneInfo(psg);

        int numplane= myList.size();

        JPanel flightsinfojp = new JPanel(new GridLayout(numplane,1,10,10));
        JPanel[] infop = new JPanel[numplane];
        JLabel[] info1 = new JLabel[numplane];
        JLabel[] info2 = new JLabel[numplane];
        JLabel[] info3 = new JLabel[numplane];
        for(int ii = 0; ii < myList.size(); ii++)
        {
            infop[ii] = new JPanel(new GridLayout(3,1));
            String infonum = myList.get(ii).getNumber();
            String infosetuptime = myList.get(ii).getSetupTime();
            String infolandTime = myList.get(ii).getLandTine();
            String infost= myList.get(ii).getSetupTerminal();
            String infolt = myList.get(ii).getLandTerminal();
            info1[ii] = new JLabel("        "+infonum+"              "+"2022-3-15");
            info2[ii] = new JLabel("          "+infosetuptime+"  -------->   "+infolandTime);
            info3[ii] = new JLabel("        "+infost+"   -------->  "+infolt);
            Font fnt9 = new Font("Serief", Font.BOLD, 20);// Setting the size and style of the font.
            info1[ii].setFont(fnt9);
            info2[ii].setFont(fnt9);
            info3[ii].setFont(fnt9);
            infop[ii].add(info1[ii]);
            infop[ii].add(info2[ii]);
            infop[ii].add(info3[ii]);
            infop[ii].setBackground(Color.white);
            flightsinfojp.add(infop[ii]);
        }
        flightsinfojp.setOpaque(true);
        flightsinfojp.setBackground(qianlan);
        panel1.add(flightsinfojp);

        JRadioButton[] rb = new JRadioButton[numplane];
        JPanel panel2 = new JPanel(new GridLayout(numplane,1));
        for(int k=0;k<numplane;k++){
            rb[k]=new JRadioButton("Flight ID   "+myList.get(k).getNumber());
            Font fnt22 = new Font("Serief", Font.BOLD, 10);// Setting the size and style of the font.
            rb[k].setFont(fnt22);
            group.add(rb[k]);
            panel2.add(rb[k]);
        }
        panel2.setOpaque(true);
        panel1.add(panel2);
        JButton btn = new JButton("Go");
        //panel1.add(btn);
        jfra.setBounds(300, 300, background.getIconWidth(), background.getIconHeight());
        Dimension d = jfra.getSize();
        int width,height;
        width = d.width;
        height = d.height;
        System.out.println("w"+width+"h"+height);
        panel1.setLocation(200,300);
        ImageIcon staffimg = new ImageIcon("staff.jpg");
        JLabel stafflabel = new JLabel(staffimg);
        panel0.add(BorderLayout.CENTER,panel1);
        panel0.add(BorderLayout.SOUTH,btn);
        panel0.setOpaque(true);

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)   {
                int k,s=0;
                int t=rb.length;
                for(k=0;k<t;k++)
                {if(rb[k].isSelected()==true)
                { s=1;
                  LogIn.currentPlaneNum=myList.get(k).getNumber();
                  }
                }
                if(s==1){
                    System.out.println(LogIn.currentPlaneNum);
                    jfra.setVisible(false);
                    new www();
                }
                else
                {JOptionPane.showMessageDialog(null, "Please tell us your flight!");  }
            }
        });

        myPanel.add(jpan1);
        //myPanel.add(label1);
        myPanel.add(panel0);
        myPanel.add(stafflabel);
        jfra.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
        jfra.setBounds(300, 300, background.getIconWidth(), background.getIconHeight());
        jfra.setVisible(true);
        jfra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
}
